
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 09/29/2012 22:10:44
-- Generated from EDMX file: C:\Users\ss\Documents\Visual Studio 2010\Projects\Arcomage\trunk\Server\DatabaseModel.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [ArcomageDB];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_RoomChatUser]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[RoomChats] DROP CONSTRAINT [FK_RoomChatUser];
GO
IF OBJECT_ID(N'[dbo].[FK_RoomChatUser1]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[RoomChats] DROP CONSTRAINT [FK_RoomChatUser1];
GO
IF OBJECT_ID(N'[dbo].[FK_SourceResourse]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Sources] DROP CONSTRAINT [FK_SourceResourse];
GO
IF OBJECT_ID(N'[dbo].[FK_CardResourse]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Cards] DROP CONSTRAINT [FK_CardResourse];
GO
IF OBJECT_ID(N'[dbo].[FK_DeckCard_Deck]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[DeckCard] DROP CONSTRAINT [FK_DeckCard_Deck];
GO
IF OBJECT_ID(N'[dbo].[FK_DeckCard_Card]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[DeckCard] DROP CONSTRAINT [FK_DeckCard_Card];
GO
IF OBJECT_ID(N'[dbo].[FK_GameUser]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Games] DROP CONSTRAINT [FK_GameUser];
GO
IF OBJECT_ID(N'[dbo].[FK_GameUser1]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Games] DROP CONSTRAINT [FK_GameUser1];
GO
IF OBJECT_ID(N'[dbo].[FK_GameGameCondition]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Games] DROP CONSTRAINT [FK_GameGameCondition];
GO
IF OBJECT_ID(N'[dbo].[FK_GameGameChat]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Games] DROP CONSTRAINT [FK_GameGameChat];
GO
IF OBJECT_ID(N'[dbo].[FK_GameChatUser]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[GameChats] DROP CONSTRAINT [FK_GameChatUser];
GO
IF OBJECT_ID(N'[dbo].[FK_MoveGame]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Moves] DROP CONSTRAINT [FK_MoveGame];
GO
IF OBJECT_ID(N'[dbo].[FK_MoveUser]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Moves] DROP CONSTRAINT [FK_MoveUser];
GO
IF OBJECT_ID(N'[dbo].[FK_MoveCard]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Moves] DROP CONSTRAINT [FK_MoveCard];
GO
IF OBJECT_ID(N'[dbo].[FK_MoveAction]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Moves] DROP CONSTRAINT [FK_MoveAction];
GO
IF OBJECT_ID(N'[dbo].[FK_TestGSResourse]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Resourses] DROP CONSTRAINT [FK_TestGSResourse];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Users]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Users];
GO
IF OBJECT_ID(N'[dbo].[News]', 'U') IS NOT NULL
    DROP TABLE [dbo].[News];
GO
IF OBJECT_ID(N'[dbo].[RoomChats]', 'U') IS NOT NULL
    DROP TABLE [dbo].[RoomChats];
GO
IF OBJECT_ID(N'[dbo].[Resourses]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Resourses];
GO
IF OBJECT_ID(N'[dbo].[Sources]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Sources];
GO
IF OBJECT_ID(N'[dbo].[Cards]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Cards];
GO
IF OBJECT_ID(N'[dbo].[Decks]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Decks];
GO
IF OBJECT_ID(N'[dbo].[Games]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Games];
GO
IF OBJECT_ID(N'[dbo].[GameConditions]', 'U') IS NOT NULL
    DROP TABLE [dbo].[GameConditions];
GO
IF OBJECT_ID(N'[dbo].[GameChats]', 'U') IS NOT NULL
    DROP TABLE [dbo].[GameChats];
GO
IF OBJECT_ID(N'[dbo].[Moves]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Moves];
GO
IF OBJECT_ID(N'[dbo].[Actions]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Actions];
GO
IF OBJECT_ID(N'[dbo].[TestGS]', 'U') IS NOT NULL
    DROP TABLE [dbo].[TestGS];
GO
IF OBJECT_ID(N'[dbo].[DeckCard]', 'U') IS NOT NULL
    DROP TABLE [dbo].[DeckCard];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Users'
CREATE TABLE [dbo].[Users] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Login] nvarchar(max)  NOT NULL,
    [Password] nvarchar(max)  NOT NULL,
    [Email] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'News'
CREATE TABLE [dbo].[News] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Text] nvarchar(max)  NOT NULL,
    [Time] datetime  NOT NULL
);
GO

-- Creating table 'RoomChats'
CREATE TABLE [dbo].[RoomChats] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Time] datetime  NOT NULL,
    [Text] nvarchar(max)  NOT NULL,
    [From_Id] int  NOT NULL,
    [To_Id] int  NULL
);
GO

-- Creating table 'Resourses'
CREATE TABLE [dbo].[Resourses] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [TestGSId] int  NOT NULL
);
GO

-- Creating table 'Sources'
CREATE TABLE [dbo].[Sources] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Resourse_Id] int  NOT NULL
);
GO

-- Creating table 'Cards'
CREATE TABLE [dbo].[Cards] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Action] nvarchar(max)  NOT NULL,
    [Cost] int  NOT NULL,
    [Resourse_Id] int  NOT NULL
);
GO

-- Creating table 'Decks'
CREATE TABLE [dbo].[Decks] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'Games'
CREATE TABLE [dbo].[Games] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Time] datetime  NOT NULL,
    [Creator_Id] int  NOT NULL,
    [SecondPlayer_Id] int  NOT NULL,
    [GameCondition_Id] int  NOT NULL,
    [GameChat_Id] int  NOT NULL
);
GO

-- Creating table 'GameConditions'
CREATE TABLE [dbo].[GameConditions] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'GameChats'
CREATE TABLE [dbo].[GameChats] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Time] datetime  NOT NULL,
    [Text] nvarchar(max)  NOT NULL,
    [User_Id] int  NOT NULL
);
GO

-- Creating table 'Moves'
CREATE TABLE [dbo].[Moves] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Game_Id] int  NOT NULL,
    [User_Id] int  NOT NULL,
    [Card_Id] int  NOT NULL,
    [Action_Id] int  NOT NULL
);
GO

-- Creating table 'Actions'
CREATE TABLE [dbo].[Actions] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'TestGS'
CREATE TABLE [dbo].[TestGS] (
    [Id] int IDENTITY(1,1) NOT NULL
);
GO

-- Creating table 'DeckCard'
CREATE TABLE [dbo].[DeckCard] (
    [DeckCard_Card_Id] int  NOT NULL,
    [Cards_Id] int  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'Users'
ALTER TABLE [dbo].[Users]
ADD CONSTRAINT [PK_Users]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'News'
ALTER TABLE [dbo].[News]
ADD CONSTRAINT [PK_News]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'RoomChats'
ALTER TABLE [dbo].[RoomChats]
ADD CONSTRAINT [PK_RoomChats]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Resourses'
ALTER TABLE [dbo].[Resourses]
ADD CONSTRAINT [PK_Resourses]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Sources'
ALTER TABLE [dbo].[Sources]
ADD CONSTRAINT [PK_Sources]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Cards'
ALTER TABLE [dbo].[Cards]
ADD CONSTRAINT [PK_Cards]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Decks'
ALTER TABLE [dbo].[Decks]
ADD CONSTRAINT [PK_Decks]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Games'
ALTER TABLE [dbo].[Games]
ADD CONSTRAINT [PK_Games]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'GameConditions'
ALTER TABLE [dbo].[GameConditions]
ADD CONSTRAINT [PK_GameConditions]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'GameChats'
ALTER TABLE [dbo].[GameChats]
ADD CONSTRAINT [PK_GameChats]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Moves'
ALTER TABLE [dbo].[Moves]
ADD CONSTRAINT [PK_Moves]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Actions'
ALTER TABLE [dbo].[Actions]
ADD CONSTRAINT [PK_Actions]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'TestGS'
ALTER TABLE [dbo].[TestGS]
ADD CONSTRAINT [PK_TestGS]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [DeckCard_Card_Id], [Cards_Id] in table 'DeckCard'
ALTER TABLE [dbo].[DeckCard]
ADD CONSTRAINT [PK_DeckCard]
    PRIMARY KEY NONCLUSTERED ([DeckCard_Card_Id], [Cards_Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [From_Id] in table 'RoomChats'
ALTER TABLE [dbo].[RoomChats]
ADD CONSTRAINT [FK_RoomChatUser]
    FOREIGN KEY ([From_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_RoomChatUser'
CREATE INDEX [IX_FK_RoomChatUser]
ON [dbo].[RoomChats]
    ([From_Id]);
GO

-- Creating foreign key on [To_Id] in table 'RoomChats'
ALTER TABLE [dbo].[RoomChats]
ADD CONSTRAINT [FK_RoomChatUser1]
    FOREIGN KEY ([To_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_RoomChatUser1'
CREATE INDEX [IX_FK_RoomChatUser1]
ON [dbo].[RoomChats]
    ([To_Id]);
GO

-- Creating foreign key on [Resourse_Id] in table 'Sources'
ALTER TABLE [dbo].[Sources]
ADD CONSTRAINT [FK_SourceResourse]
    FOREIGN KEY ([Resourse_Id])
    REFERENCES [dbo].[Resourses]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_SourceResourse'
CREATE INDEX [IX_FK_SourceResourse]
ON [dbo].[Sources]
    ([Resourse_Id]);
GO

-- Creating foreign key on [Resourse_Id] in table 'Cards'
ALTER TABLE [dbo].[Cards]
ADD CONSTRAINT [FK_CardResourse]
    FOREIGN KEY ([Resourse_Id])
    REFERENCES [dbo].[Resourses]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CardResourse'
CREATE INDEX [IX_FK_CardResourse]
ON [dbo].[Cards]
    ([Resourse_Id]);
GO

-- Creating foreign key on [DeckCard_Card_Id] in table 'DeckCard'
ALTER TABLE [dbo].[DeckCard]
ADD CONSTRAINT [FK_DeckCard_Deck]
    FOREIGN KEY ([DeckCard_Card_Id])
    REFERENCES [dbo].[Decks]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Cards_Id] in table 'DeckCard'
ALTER TABLE [dbo].[DeckCard]
ADD CONSTRAINT [FK_DeckCard_Card]
    FOREIGN KEY ([Cards_Id])
    REFERENCES [dbo].[Cards]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_DeckCard_Card'
CREATE INDEX [IX_FK_DeckCard_Card]
ON [dbo].[DeckCard]
    ([Cards_Id]);
GO

-- Creating foreign key on [Creator_Id] in table 'Games'
ALTER TABLE [dbo].[Games]
ADD CONSTRAINT [FK_GameUser]
    FOREIGN KEY ([Creator_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_GameUser'
CREATE INDEX [IX_FK_GameUser]
ON [dbo].[Games]
    ([Creator_Id]);
GO

-- Creating foreign key on [SecondPlayer_Id] in table 'Games'
ALTER TABLE [dbo].[Games]
ADD CONSTRAINT [FK_GameUser1]
    FOREIGN KEY ([SecondPlayer_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_GameUser1'
CREATE INDEX [IX_FK_GameUser1]
ON [dbo].[Games]
    ([SecondPlayer_Id]);
GO

-- Creating foreign key on [GameCondition_Id] in table 'Games'
ALTER TABLE [dbo].[Games]
ADD CONSTRAINT [FK_GameGameCondition]
    FOREIGN KEY ([GameCondition_Id])
    REFERENCES [dbo].[GameConditions]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_GameGameCondition'
CREATE INDEX [IX_FK_GameGameCondition]
ON [dbo].[Games]
    ([GameCondition_Id]);
GO

-- Creating foreign key on [GameChat_Id] in table 'Games'
ALTER TABLE [dbo].[Games]
ADD CONSTRAINT [FK_GameGameChat]
    FOREIGN KEY ([GameChat_Id])
    REFERENCES [dbo].[GameChats]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_GameGameChat'
CREATE INDEX [IX_FK_GameGameChat]
ON [dbo].[Games]
    ([GameChat_Id]);
GO

-- Creating foreign key on [User_Id] in table 'GameChats'
ALTER TABLE [dbo].[GameChats]
ADD CONSTRAINT [FK_GameChatUser]
    FOREIGN KEY ([User_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_GameChatUser'
CREATE INDEX [IX_FK_GameChatUser]
ON [dbo].[GameChats]
    ([User_Id]);
GO

-- Creating foreign key on [Game_Id] in table 'Moves'
ALTER TABLE [dbo].[Moves]
ADD CONSTRAINT [FK_MoveGame]
    FOREIGN KEY ([Game_Id])
    REFERENCES [dbo].[Games]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_MoveGame'
CREATE INDEX [IX_FK_MoveGame]
ON [dbo].[Moves]
    ([Game_Id]);
GO

-- Creating foreign key on [User_Id] in table 'Moves'
ALTER TABLE [dbo].[Moves]
ADD CONSTRAINT [FK_MoveUser]
    FOREIGN KEY ([User_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_MoveUser'
CREATE INDEX [IX_FK_MoveUser]
ON [dbo].[Moves]
    ([User_Id]);
GO

-- Creating foreign key on [Card_Id] in table 'Moves'
ALTER TABLE [dbo].[Moves]
ADD CONSTRAINT [FK_MoveCard]
    FOREIGN KEY ([Card_Id])
    REFERENCES [dbo].[Cards]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_MoveCard'
CREATE INDEX [IX_FK_MoveCard]
ON [dbo].[Moves]
    ([Card_Id]);
GO

-- Creating foreign key on [Action_Id] in table 'Moves'
ALTER TABLE [dbo].[Moves]
ADD CONSTRAINT [FK_MoveAction]
    FOREIGN KEY ([Action_Id])
    REFERENCES [dbo].[Actions]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_MoveAction'
CREATE INDEX [IX_FK_MoveAction]
ON [dbo].[Moves]
    ([Action_Id]);
GO

-- Creating foreign key on [TestGSId] in table 'Resourses'
ALTER TABLE [dbo].[Resourses]
ADD CONSTRAINT [FK_TestGSResourse]
    FOREIGN KEY ([TestGSId])
    REFERENCES [dbo].[TestGS]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_TestGSResourse'
CREATE INDEX [IX_FK_TestGSResourse]
ON [dbo].[Resourses]
    ([TestGSId]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------